package com.uzai.console.service.sysmaterial.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.entity.User;
import com.uzai.common.enums.IdentityEnum;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.profit.normalrewardrule.NormalRewardRuleQuery;
import com.uzai.console.dto.sysmaterial.SysMaterialDto;
import com.uzai.console.dto.sysmaterial.SysMaterialIdDto;
import com.uzai.console.dto.sysmaterial.SysMaterialQuery;
import com.uzai.console.dto.sysmaterial.SysMaterialUpdateDto;
import com.uzai.console.dto.user.UserQuery;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.entity.NormalRewardRule;
import com.uzai.console.entity.SysMaterial;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.mapper.NormalRewardRuleMapper;
import com.uzai.console.mapper.SysMaterialMapper;
import com.uzai.console.mapper.WechatUserMapper;
import com.uzai.console.service.qiniu.QiniuService;
import com.uzai.console.service.sysmaterial.SysMaterialService;
import com.uzai.console.service.tencent.TencentCosService;
import com.uzai.console.service.user.UserService;
import com.uzai.console.vo.sysmaterial.SysMaterialVo;
import com.uzai.console.vo.sysmaterial.UploadFileVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年09月01日 19:26
 */
@Service
public class SysMaterialServiceImpl implements SysMaterialService {

    @Autowired
    private SysMaterialMapper sysMaterialMapper;
    @Autowired
    private QiniuService qiniuService;
    @Autowired
    private TencentCosService tencentCosService;
    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;

    /**
     * 分页查询会员类型列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param sysMaterialQuery
     * @return null
     */
    public Page<SysMaterialVo> findSysMaterialList(SysMaterialQuery sysMaterialQuery){
        //新建返回到页面的LIST对象
        List<SysMaterialVo> SysMaterialVoList = new ArrayList<>();

        //执行分页查询对象
        Page<SysMaterial> page = new Page<>();
        sysMaterialQuery.convert(page);
        List<SysMaterial> sysMaterialList = sysMaterialMapper.findByList(page, sysMaterialQuery);

        if(sysMaterialList != null){
            for (SysMaterial sysMaterial : sysMaterialList){
                SysMaterialVo SysMaterialVo = new SysMaterialVo();
                BeanUtils.copyProperties(sysMaterial, SysMaterialVo);
                SysMaterialVoList.add(SysMaterialVo);
            }
        }

        //返回到页面分页对象
        Page<SysMaterialVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(SysMaterialVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询系统素材详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param sysMaterialIdDto
     * @return null
     */
    public SysMaterialVo findSysMaterialInfo(SysMaterialIdDto sysMaterialIdDto){
        if(sysMaterialIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择素材id");
        }

        SysMaterial sysMaterial = sysMaterialMapper.selectById(sysMaterialIdDto.getId(), sysMaterialIdDto.getMerId());

        if(sysMaterial == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此素材不存在");
        }

        SysMaterialVo SysMaterialVo = new SysMaterialVo();
        BeanUtils.copyProperties(sysMaterial, SysMaterialVo);

        return SysMaterialVo;

    }

    /**
     * 删除会员类型
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param sysMaterialIdDto
     * @return null
     */
    public void deleteSysMaterial(SysMaterialIdDto sysMaterialIdDto){

        if(sysMaterialIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择素材id");
        }

        SysMaterial sysMaterial = sysMaterialMapper.selectById(sysMaterialIdDto.getId(), sysMaterialIdDto.getMerId());

        if(sysMaterial == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此素材不存在");
        }

        //删除
        sysMaterialMapper.deleteById(sysMaterialIdDto.getId(), sysMaterial.getMerId());
    }

    /**
     * 添加系统素材
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param sysMaterialDto
     * @return null
     */
    public void addSysMaterial(SysMaterialDto sysMaterialDto){
        if(sysMaterialDto.getSpace() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入云空间类型");
        }

        if(sysMaterialDto.getType() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入文件类型");
        }

        if(StringUtils.isBlank(sysMaterialDto.getPath())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入文件存储路径");
        }

        SysMaterial sysMaterial = new SysMaterial();
        BeanUtils.copyProperties(sysMaterialDto, sysMaterial);
        Integer now = DateUtil.getNowTime();
        sysMaterial.setId(IdWorker.getId());
        sysMaterial.setCreateTime(now);
        sysMaterial.setUpdateTime(now);
        //添加
        sysMaterialMapper.insert(sysMaterial);
    }

    /**
     * 修改系统素材
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param sysMaterialUpdateDto
     * @return null
     */
    public void updateSysMaterial(SysMaterialUpdateDto sysMaterialUpdateDto){

        if(sysMaterialUpdateDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入文件id");
        }

        if(StringUtils.isBlank(sysMaterialUpdateDto.getFilename())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入文件名称");
        }

        SysMaterial sysMaterial = sysMaterialMapper.selectById(sysMaterialUpdateDto.getId(), sysMaterialUpdateDto.getMerId());

        if(sysMaterial == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此素材不存在");
        }


        sysMaterial.setFilename(sysMaterialUpdateDto.getFilename());
        sysMaterial.setUpdateTime(DateUtil.getNowTime());

        //修改
        sysMaterialMapper.updateById(sysMaterial);
    }


    /**
     * 上传文件
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return CmsActivityInfoVo
     */
    public UploadFileVo uploadFile(MultipartFile file, Long merId){
        UploadFileVo uploadFileVo = new UploadFileVo();

        //获取运营商配置的上传文件存储类型
        MerBaseConfig merBaseConfig_fileSaveType = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.FILE_SAVE_TYPE.getValue());
        if(merBaseConfig_fileSaveType == null || StringUtils.isBlank(merBaseConfig_fileSaveType.getVal())) { //还未配置，请先配置存储空间
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请先设置云存储方式");
        }

        Integer fileSaveType = Integer.valueOf(merBaseConfig_fileSaveType.getVal());
        if(fileSaveType.intValue() == 1){ //使用七牛云
            String fileUrl =  qiniuService.uploadFile(file, merId);     //上传图片
            uploadFileVo.setPathUrl(fileUrl);
        }else if(fileSaveType.intValue() == 2){ //腾讯云
            String fileUrl =  tencentCosService.uploadFile(file, merId); //上传图片
            uploadFileVo.setPathUrl(fileUrl);
        }else{
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请先设置默认的上传云存储类型");
        }
        return uploadFileVo;
    }

    /**
     * @author lucher
     * @date 2022/3/9 15:40
     * @param merId
     * @param inputStream
     * @param fileName
     * @return java.lang.String
     */
    @Override
    public UploadFileVo upload(Long merId, InputStream inputStream, String fileName) {
        UploadFileVo uploadFileVo = new UploadFileVo();

        //获取运营商配置的上传文件存储类型
        MerBaseConfig merBaseConfig_fileSaveType = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.FILE_SAVE_TYPE.getValue());
        if(merBaseConfig_fileSaveType == null || StringUtils.isBlank(merBaseConfig_fileSaveType.getVal())) { //还未配置，请先配置存储空间
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请先设置素材管理存储方式");
        }

        Integer fileSaveType = Integer.valueOf(merBaseConfig_fileSaveType.getVal());
        if(fileSaveType.intValue() == 1){ //使用七牛云
            String fileUrl =  qiniuService.upload(merId, inputStream, fileName);
            uploadFileVo.setPathUrl(fileUrl);
        }else if(fileSaveType.intValue() == 2){ //腾讯云
            String fileUrl =  tencentCosService.upload(merId, inputStream, fileName);
            uploadFileVo.setPathUrl(fileUrl);
        }else{
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请先设置默认的上传云存储类型");
        }
        return uploadFileVo;
    }

}
